﻿HmJS.$Import('crypt.array.cryptographyrmd160');
HmJS.$Import('crypt.array.cryptographyhex');
HmJS.$Import('crypt.string.cryptographyutf8');
HmJS.$Import('crypt.string.cryptographyhex');
HmJS.$Import('crypt.string.cryptographycommon');

/*
---
name: String.RMD160
version: 0.3-1
description: String RMD160 hashing.
license: MIT-style
download: http://mootools.net/forge/p/string_cryptography
source: http://github.com/sixtyseconds/mootools-string-cryptography
authors: [Christopher Pitt, Enrique Erne]
requires: 
- Core/String
- String.toUTF8
- String.toHex
- String.toBin
- Array.toRMD160
- Array.toHex
provides: [String.toRMD160]
...
*/

HmJS.register('crypt.string.cryptographyrmd160', function ($ns) {

	var rmd160 = function (string, size, hexUpperCase) {
		var bin = string.toUTF8().toBin(size, true),
				proc = bin.toRMD160(string.length * size);

		return binToString(proc).toHex(hexUpperCase);
	};

	var binToString = function (array) {
		var result = '';

		for (var a = 0, len = array.length * 32; a < len; a += 8) {
			result += String.fromCharCode((array[a >> 5] >>> (a % 32)) & 0xFF);
		}

		return result;
	};

	String.implement({
		'toRMD160': function () {
			return rmd160(this, 8, 0);
		}
	});

});
